<?php

class WebIndexService extends BaseService
{

    private static $self           =   NULL;
    private $mUser                 =   NULL;
    private $mUserMessage          =   NULL;
    private $mUserAward            =   NULL;
    private $mUserTicket           =   NULL;
    private $mDealDeposit          =   NULL;
    private $mUserFund             =   NULL;
    private $mUserLock             =   NULL;
    private $mRefundPlan           =   NULL;
    private $mBankCard             =   NULL;
    private $mExperRecord          =   NULL;
    private $mInvest               =   NULL;
    private $mDealRecord           =   NULL;




    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }
        return self::$self;
    }


    public function __construct()
    {

        $this->mUser                   =   new Involved_UserModel();
        $this->mUserMessage            =   new Involved_UserMessageModel();
        $this->mUserTicket             =   new Involved_UserTicketModel();
        $this->mUserAward              =   new Involved_UserAwardModel();
        $this->mDealDeposit            =   new Involved_DealDepositModel();
        $this->mUserFund               =   new Involved_UserFundModel();
        $this->mUserLock               =   new Involved_UserLockModel();
        $this->mRefundPlan             =   new Involved_RefundPlanModel();
        $this->mBankCard               =   new Involved_BankCardModel();
        $this->mExperRecord            =   new Involved_ExperRecordModel();
        $this->mInvest                 =   new Involved_InvestModel();
        $this->mDealRecord           =   new Involved_DealRecordModel();
    }


    /**
     *web登陆
     *xf
     **/
    public  function WebUserlogin($paramObj){
        //php判断是否为空
        if(!preg_match("/^1[34578]{1}\d{9}$/",$paramObj->phones)) {
            $mess['code'] = '2';
            $mess['mess'] = '请输入正确的手机号';
            return $mess;
        }
        //php是否存在
        $data=$this->mUser->UserFirphone($paramObj->phones);
        if(empty($data->user_id)){
            $mess['code'] = '2';
            $mess['mess'] = '用户不存在';
            return $mess;
        }
        //是不是后台禁止登陆
        if($data->is_locked=='1'){
            $mess['code'] = '2';
            $mess['mess'] = '对不起，您的账号已被限制，请联系客服';
            return $mess;
        }
        $Lookcishu  =$this->mUserLock->LockUsercishu($data->user_id);//获取登陆次数
        if(!empty($Lookcishu)){
            $daes=strtotime(date('Ymd',$Lookcishu->lock_time)) + 86400;
            if(time()>$daes){
                $LookUseraaaa=$this->mUserLock->delete(['user_id'=>$Lookcishu->user_id]);
            }
        }
        $LookUsers  =$this->mUserLock->LockUser($data->user_id);//获取人是不是本冻结
        if(!empty($LookUsers)){//如果被冻结，看是否到解锁时间
            $LookUsersa  =$this->mUserLock->LockUserfirst ($data->user_id);
            if(time()<$LookUsersa->lock_time){
                $mess['code'] = '2';
                $mess['mess'] = '对不起，您的账户已被锁定,请于'.date('H:i:s',$LookUsersa->lock_time).'后尝试登录！';
                return $mess;
            }else{
                $LookUseraaaa=$this->mUserLock->delete(['user_id'=>$data->user_id]);
            }
        }
        //加密判断
        $psswd=UserEnum::get(md5(md5($paramObj->pwd)),$data->salt);//加密方式
        if($psswd==$data->password){
            $date = $this->mUser->UserInfofirst($data->user_id);
            #存储SESSION
            Session::put(UserEnum::USER_INFO_USER_ID, $data->user_id);
            Session::put(UserEnum::USER_INFO_SESSION_KEY, $data);
            Session::forget('webtoken');
            Session::forget('weburl');//
            $mess['code'] = '1';
            $mess['mess'] = '登陆成功';
            return $mess;
        }else{
            $LookUser =$this->mUserLock->LockUserfirst($data->user_id);

            if(!empty($LookUser->number)){
                $count = $LookUser->number;
                if($count<= 4){
                    $count--;
                    $logins=$count;
                    $array=array(
                        'number'    =>$logins,
                        'user_id'   =>$data->user_id,
                        'lock_time'  =>time(),
                        'ip'        =>$_SERVER['REMOTE_ADDR'],
                    );
                    if ($count>=0&&$count<=4) {
                        $LookUserd= $this->mUserLock->update($array,['lock_id' => $LookUser->lock_id]);
                    }
                }
                if($count == 0){
                    $data4['number'] = $count;
                    $data4['user_id'] = $data->user_id;
                    $data4['lock_time'] = time()+60*30;
                    $data4['ip'] = $_SERVER['REMOTE_ADDR'];
                    $LookUserd= $this->mUserLock->update($data4,['lock_id' => $LookUser->lock_id]);
                    if (!empty($LookUserd)) {
                        $mess['code'] = '2';
                        $mess['mess'] = '您已经连续5次输入错误密码，账户被锁定30分钟';
                        return $mess;
                    }
                }
                $mess['code'] = '3';
                $mess['mess'] = '"密码有误，请重新输入,今天还剩'.$count.'次';
                return $mess;
            }else{
                $logins=4;
                $array=array(
                    'number'    =>$logins,
                    'user_id'   => $data->user_id,
                    'lock_time'  =>time(),
                    'ip'        =>$_SERVER['REMOTE_ADDR'],
                );
                $LookUserd= $this->mUserLock->insert($array);
            }
            if (empty($LookUser->number)){
                $mess['code'] = '3';
                $mess['mess'] = "密码有误，请重新输入，今天还剩4次机会";
                return $mess;
            }

        }

    }
    /**
     *web
     *xf
     **/
    public function  WebforegetSetPwd($paramObj){
        if($paramObj->newpwd!=$paramObj->surepwd){
            $mess['code'] = '2';
            $mess['mess'] = '对不起，两次输入密码不一致';
            return $mess;
        }
        //新的密码加密
        $salt  =  rand(100000, 999999);
        $newpsswd=UserEnum::get(md5(md5($paramObj->newpwd)),$salt);//加密方式
        $pwddata=array(
            'password'=>$newpsswd,
            'salt'=>$salt,
        );

        $user_id =session_get(UserEnum::USER_INFO_USER_ID);
        $data=$this->mUser->UserFirphone($paramObj->phone);
        $deta= $this->mUser->update($pwddata,['user_id'=>$data->user_id]);
        if(!empty($deta)){
            $mess['code'] = '1';
            $mess['mess'] = '修改成功';
            return $mess;
        }else{
            $mess['code'] = '2';
            $mess['mess'] = '修改失败';
            return $mess;
        }
    }






    public function RegisterNext($paramObj)
    {

    }


}


